package client

import (
	"base"
	"gate_server/logic/center/api"
	"gate_server/logic/game/api"
	. "gate_server/logic/internal"
	"gate_server/model"
	"leaf/gate"
	"proto/player"
	"proto/server/center"
)

type clientService struct {
}

func init() {
	service := new(clientService)

	Protocol.Register(new(proto_player.Login), service.login)
	Protocol.Register(new(proto_player.Logout), service.logout)
	Protocol.Register(new(proto_player.Request), service.request)
}

func (*clientService) login(args []interface{}) {
	msg := args[0].(*proto_player.Login)
	base.Logger.Debug("player login, msg: %s", msg)

	agent := args[1].(gate.Agent)
	client := agent.UserData().(*model.Client)

	request := new(proto_server_center.Login)
	request.Account = msg.Account
	request.Password = msg.Password
	request.ClientId = client.Id()
	center_api.WriteMsg(request)
}

func (*clientService) logout(args []interface{}) {
	base.Logger.Debug("player logout")

	agent := args[1].(gate.Agent)
	client := agent.UserData().(*model.Client)
	system.logout(client)
}

func (*clientService) request(args []interface{}) {
	request := args[0].(*proto_player.Request)
	agent := args[1].(gate.Agent)

	client := agent.UserData().(*model.Client)

	var playerId base.PlayerId
	if player := client.Player(); player == nil {
		base.Logger.Release("客户端未登录")
		base.Logger.Release("proto name: %v", request.Name)
		return
	} else {
		playerId = player.Id()
	}

	game_api.Request(playerId, request.Name, request.Data)
}
